#include "Model.h"
class Game;

Model::Model(){
	
	mTransMatrix = new D3DXMATRIX();
	mScaleMatrix = new D3DXMATRIX();
	mRotXMatrix= new D3DXMATRIX();
	mRotYMatrix= new D3DXMATRIX();;
	mRotZMatrix= new D3DXMATRIX();;
	mRotMatrix = new D3DXMATRIX();

	mScale.x = 1.0f;
	mScale.y = 1.0f;
	mScale.z = 1.0f;

	mRot.x = 0.0f;
	mRot.y = 0.0f;
	mRot.z = 0.0f;

	mPos.x = 0.0f;
	mPos.y = 0.0f;
	mPos.z = 0.0f;
	
	count =0.0f;
 
	::D3DXMatrixRotationYawPitchRoll(mRotMatrix,mRot.x,mRot.y,mRot.z);
	//_mDefault.x
	mMesh = new Mesh("Default","_mDefault.x");
}

Model::Model(string pName, string pTextureLoc){

	mName = pName;

	mTransMatrix = new D3DXMATRIX();
	mScaleMatrix = new D3DXMATRIX();
	mRotXMatrix= new D3DXMATRIX();
	mRotYMatrix= new D3DXMATRIX();;
	mRotZMatrix= new D3DXMATRIX();;
	mRotMatrix= new D3DXMATRIX();;

	mScale.x = 1.0f;
	mScale.y = 1.0f;
	mScale.z = 1.0f;

	mRot.x = 0.0f;
	mRot.y = 0.0f;
	mRot.z = 0.0f;

	mPos.x = 0.0f;
	mPos.y = 0.0f;
	mPos.z = 0.0f;


	//Set default material properties
	count =0.0f;
	

	::D3DXMatrixRotationYawPitchRoll(mRotMatrix,mRot.x,mRot.y,mRot.z);
	
	//::D3DXQuaternionNormalize(mQuat,mQuat);
	

	mMesh = new Mesh(pName,pTextureLoc);
}

void Model::LoadContent(LPDIRECT3DDEVICE9 d3ddev){

	mMesh->LoadMesh(d3ddev);
}

void Model::Update(LPDIRECT3DDEVICE9 d3ddev, double pElapsedTime, double pTotal){
	
	for(int a =0; a < mAnimations.size();a ++){
		mAnimations[a]->Update(d3ddev,pElapsedTime,0.0);
	}

	int index = -1;
	for(int a =0; a < mKeyFrames.size();a ++){
		if(mKeyFrames[a]->GetTFinal() > pTotal){
			if(index < 0 ){
					index = a;
			}
			else if (mKeyFrames[a]->GetTFinal() < mKeyFrames[index]->GetTFinal()){
					index = a;
			}
		}
	}

	if(index >=0){
		if(index ==1){
			index = index;
		}
		mKeyFrames[index]->Update(d3ddev,&mPos,&mRot,mRotMatrix,pElapsedTime,pTotal);
	}
	else{
//		::D3DXMatrixRotationYawPitchRoll(mRotMatrix,mRot.x,mRot.y,mRot.z);
	}
}

Mesh* Model::GetMesh(){
	return mMesh;
}

string Model::GetName(){
	return mName;
}

D3DXVECTOR3* Model::GetPos(){
	return &mPos;
}

D3DXVECTOR3* Model::GetRot(){
	return &mRot;
}
D3DXVECTOR3* Model::GetScale(){
	return &mScale;
}

D3DXQUATERNION* Model::GetQuat(){
	return mQuat;
}

void Model::SetQuat(D3DXQUATERNION* pQuat){
	mQuat = pQuat;
}

void Model::SetName(string pName){
	mName = pName;
}
void Model::SetMesh(Mesh* pMesh){
	mMesh = pMesh;
}
void Model::SetRot(D3DVECTOR* pRot){
	mRot = *(pRot);
}
void Model::SetScale(D3DVECTOR* pScale){
	mScale = *(pScale);
}
void Model::SetPos(D3DXVECTOR3* pPos){
	mPos = *(pPos);
}

//Returns a matrix with all the transformations for the object
D3DXMATRIX* Model::GetTransMatrix(){

	if(count <1 ){
		count += 0.01f;
	}
	else{
		count = 0.0f;
	}


	//D3DXQUATERNION* mNew = new D3DXQUATERNION();
	//mNew->w = 90;
	//mNew->x = 0;
	//mNew->y = 1;
	//mNew->z = 0;
	//::D3DXQuaternionNormalize(mNew,mNew);

	//D3DXQuaternionSlerp( mQuat , mQuat,mNew , count);
	//::D3DXQuaternionNormalize(mQuat,mQuat);
	//*mQuat += *mNew;
	//::D3DXQuaternionNormalize(mQuat,mQuat);

	

	//D3DXMATRIX mRotMat;
	//::D3DXMatrixRotationYawPitchRoll(&mRotMat,mRot.x,mRot.y,mRot.z);
	//D3DXMatrixRotationQuaternion(&mRotMat,mQuat);
	D3DXMatrixRotationYawPitchRoll(mRotMatrix,mRot.x,mRot.y,mRot.z);
	D3DXMatrixScaling(mScaleMatrix,mScale.x,mScale.y,mScale.z);
	D3DXMatrixTranslation(mTransMatrix,mPos.x,mPos.y, mPos.z);
	
	return &( *(mRotMatrix) * *(mScaleMatrix) * *(mTransMatrix));
}

void Model::Draw(LPDIRECT3DDEVICE9 d3ddev){
	D3DXMATRIX* mTransforms = GetTransMatrix();
	
	d3ddev->SetTransform(D3DTS_WORLD, mTransforms);
	//d3ddev->SetMaterial(&mMaterial);
	mMesh->Draw(d3ddev);
}

void Model::AddKeyFrame(KeyFrame* pKey){
	mKeyFrames.push_back(pKey);
}

void Model::Release(){
	mMesh->Release();

}
void Model::AddAnimation(Animation* pAnim){
	mAnimations.push_back(pAnim);
}


